package maskword

import (
	"fmt"

	"log"

	ac "github.com/anknown/ahocorasick"
)

var m *ac.Machine

func Init() {
	dict := _dict
	m = new(ac.Machine)
	if err := m.Build(dict); err != nil {
		log.Fatal(err)
		return
	}
	log.Fatalf("init mask word match ok %d", len(dict))
}

type Terms []*ac.Term

func (t Terms) String() string {
	var str string = "[ "
	for _, _t := range t {
		str += fmt.Sprintf("{%d:%s} ", _t.Pos, string(_t.Word))
	}
	str += " ]"
	return str
}

//Search 查询是否有匹配字符
func Search(content []rune) Terms {
	terms := m.MultiPatternSearch(content, true)
	return terms
}

//Match 精确匹配
func Match(content []rune) bool {
	terms := m.ExactSearch(content)
	return len(terms) > 0
}
